﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HREDU.Hoang_Tuan.Classes;
using HREDU.Hoang_Tuan;


namespace HREDU.Hoang_Tuan
{
    public partial class frmDanhMuc : Form
    {
        public frmDanhMuc()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value">Tùy vào danh mục nào sẽ cho hiển thị tên khác</param>
        private void FormatGridStyle(bool value)
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            style.ForeColor = Color.IndianRed;
            style.BackColor = Color.Ivory;
            style.Font = new Font(dMKHACDataGridView.Font, dMKHACDataGridView.Font.Style | FontStyle.Bold);

            dMKHACDataGridView.DataSource = this.hREDUDataSet.DMKHAC.DefaultView;
            dMKHACDataGridView.Columns[0].Visible = false; //Column "L"
            dMKHACDataGridView.Columns[3].Visible = value; //Column "TENKHAC"
            dMKHACDataGridView.Columns[4].Visible = false; //Column "GHICHU"
            this.hREDUDataSet.DMKHAC.LColumn.DefaultValue = tvControl.SelectedNode.Name;

            dMKHACDataGridView.Columns[1].HeaderText = "Mã";
            dMKHACDataGridView.Columns[1].Width = 60;
            dMKHACDataGridView.Columns[2].HeaderText = tvControl.SelectedNode.Text;
            this.Text = dMKHACDataGridView.Columns[2].HeaderText;
            if (value)
            {
                dMKHACDataGridView.Columns[2].Width = 350;
                dMKHACDataGridView.Columns[3].HeaderText = "Tên khác";
                dMKHACDataGridView.Columns[3].Width = 100;
            }
            else
                dMKHACDataGridView.Columns[2].Width = 450;

            foreach (DataGridViewColumn column in dMKHACDataGridView.Columns)
            {
                column.HeaderCell.Style = style;
            }

        }
   
        private void frmDanhMuc_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'hREDUDataSet.DMKHAC' table. You can move, or remove it, as needed.
            this.dMKHACTableAdapter.Connection.ConnectionString = MITI.BaseDB.Instance.g_ConnectStr;
            InitTreeView();
            LoadDataGridView();
          
        }

        //Add node dau tien cua treeview
        public void FormatFirstNode(ref TreeNode node)
        {
            node.ForeColor = Color.Blue;
            //node.NodeFont = new Font(this.Font.Name, this.Font.Size + 1.0F, FontStyle.Bold);
            //node.NodeFont = new Font(this.Font, FontStyle.Bold);
            node.ImageIndex = 0;
            node.SelectedImageIndex = 0;
        }

        public void FormatChildNode(ref TreeNode node)
        {
            node.ForeColor = Color.DarkRed;
            //node.NodeFont = new Font(this.Font, FontStyle.Bold);
            node.ImageIndex = 0;
            node.SelectedImageIndex = 0;
        }

        public void InitTreeView()
        {
            //Lay danh muc tree view dau tien
            string strSQL;

            //Add First Node in Treeview
            TreeNode node = tvControl.Nodes.Add("", "Từ điển Danh mục");
            FormatFirstNode(ref node);
            
            strSQL = "select * from LOAIDM";

            DataSet ds = MITI.BaseDB.Instance.ExecSql_DataSet(strSQL);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                //Add Node in Treeview
                TreeNode childnode = node.Nodes.Add(dr["maloai"].ToString(), dr["ten_loaiDM"].ToString());
                //Tra ve node theo format dinh san
                FormatChildNode(ref childnode);
            }
            tvControl.ExpandAll();
            tvControl.Focus();
            tvControl.SelectedNode = tvControl.Nodes[0].Nodes[0];
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.dMKHACBindingSource.EndEdit();
                this.dMKHACTableAdapter.Update(this.hREDUDataSet.DMKHAC);
                MessageBox.Show("Cập nhật xong.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LoadDataGridView()
        {
            //Fill datagirdview loc theo loai danh muc
            this.dMKHACTableAdapter.Fill(this.hREDUDataSet.DMKHAC, tvControl.SelectedNode.Name);
            lblDanhMuc.Text = tvControl.SelectedNode.Text;
            if (tvControl.SelectedNode.Name.Trim() == "CM")
                FormatGridStyle(true);
            else
                FormatGridStyle(false);
        }

        //public void InitLoadDataGridView(TreeNode node)
        //{
        //    //Fill datagirdview loc theo loai danh muc
        //    this.dMKHACTableAdapter.Fill(this.hREDUDataSet.DMKHAC, tvControl.SelectedNode.Name);
        //    FormatGridStyleDPNX();
        //}

        private void tvControl_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
            //Kiem tra neu ko phai node tren cung thi load danh muc
            if (tvControl.SelectedNode.Parent != null)
            {
                dMKHACDataGridView.AllowUserToAddRows = true;
                LoadDataGridView();

            }
            else{
                dMKHACDataGridView.AllowUserToAddRows = false;
                //Fill datagirdview loc theo loai danh muc
                this.dMKHACTableAdapter.Fill(this.hREDUDataSet.DMKHAC, tvControl.SelectedNode.Name);
                this.Text = tvControl.Nodes[0].Text;
            }

        }
    }
}